package com.asman.worker.rn

import android.os.Handler
import android.widget.Toast
import com.facebook.react.bridge.Callback
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContextBaseJavaModule
import com.facebook.react.bridge.ReactMethod
import showToast
import java.util.*

/**
 * author : ybk
 * date   : 2019-07-18 19:47
 * desc   :
 */
class ToastModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) {

    override fun getName(): String {
        return "ToastExample"
    }

    override fun getConstants(): Map<String, Any>? {
        val constants = HashMap<String, Any>()
        constants[DURATION_SHORT_KEY] = Toast.LENGTH_SHORT
        constants[DURATION_LONG_KEY] = Toast.LENGTH_LONG
        return constants
    }

    @ReactMethod
    fun show(message: String, duration: Int) {
        showToast(message)
    }

    @ReactMethod
    fun measureLayout(successCallback: Callback) {
        showToast("倒计时3秒开始")
        Handler().postDelayed({
            showToast("回调")
            successCallback.invoke(1, 2, 3, 4)
        }, 3000)
    }

    companion object {
        private val DURATION_SHORT_KEY = "SHORT"
        private val DURATION_LONG_KEY = "LONG"
    }

}
